package servlet;

import bean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "MarkServlet", value = "/referee/MarkServlet")
public class MarkServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String goal=request.getParameter("goal");
        String rid=(String) session.getAttribute("userID");
        String markInfo = "";
        JudgeBean judgeBean=new JudgeBean();
        judgeBean.link();
        judgeBean.loadJudge();
        ApplyBean applyBean=new ApplyBean();
        applyBean.link();
        applyBean.loadApply();
        Apply app=null;
        int flag=0;
        for(Judge a:judgeBean.getJudgeArrayList()){
            if(id.equals(a.getId())&&rid.equals(a.getReferee())){
                flag=1;
            }
        }
        if(flag==1){
            for(Apply a:applyBean.getApplyArrayList()){
                if(id.equals(a.getId())&&name.equals(a.getName())){
                    app=a;

                }
            }
            app.setGrade(goal);
            applyBean.updateApply(app);
            markInfo="评分完成";
            session.setAttribute("markInfo",markInfo);
            response.sendRedirect("mark.jsp");

        }else{
            markInfo="您不是任何项目裁判";
            session.setAttribute("markInfo",markInfo);
            response.sendRedirect("mark.jsp");
        }






    }
}